package com.run2code.idea.plugin.codegenerate.component;

import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * 文件选择组件
 * 关注公众号【奔跑的码畜】，一起进步不迷路
 *
 * @author 第七人格
 * @date 2023/10/27
 */
public class FileChooserComponent {

    /**
     * 项目
     */
    private final Project project;

    public FileChooserComponent(Project project) {
        this.project = project;
    }

    /**
     * 获取实例
     *
     * @param project 项目
     * @return {@link FileChooserComponent}  文件选择组件
     */
    public static FileChooserComponent getInstance(@NotNull Project project) {
        return new FileChooserComponent(project);
    }

    /**
     * “显示文件夹选择”对话框
     *
     * @param title    标题
     * @param toSelect 默认选中文件夹
     * @param roots    根目录
     * @return {@link VirtualFile} 虚拟文件
     */
    public VirtualFile showFolderSelectionDialog(@NotNull String title, @Nullable VirtualFile toSelect, @Nullable VirtualFile... roots) {
        final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
        descriptor.setTitle(title);
        if (null != roots) {
            descriptor.setRoots(roots);
        }
        return FileChooser.chooseFile(descriptor, project, toSelect);
    }

}
